from django.utils.deprecation import MiddlewareMixin

from EventMessageWeb.enums.ApiRespCodeEnums import api_response_code
from EventMessageWeb.exception.CommonException import BusinessException
from commonTool.ApiResponse import api_response

ResultVO = api_response()


class ExceptionMiddleware(MiddlewareMixin):
    def process_exception(self, request, exception):
        """
        处理异常
        :param request: 请求
        :param exception: 异常
        :return:
        """
        # 处理异常
        if isinstance(exception, BusinessException):
            return ResultVO.new_fail(enum=exception.error_enum)
        else:
            return ResultVO.new_fail(enum=api_response_code.NETWORK_ERROR)
